package com.example.demo.controller;


import com.example.demo.pojo.Tpaper;
import com.example.demo.service.ITpaperService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Tpaper")
public class TpaperController {

    @Autowired
    private ITpaperService TpaperService;

    @RequestMapping("/selectTpaper")
    public void selectTpaper(HttpServletResponse response) throws IOException {
        List<Tpaper> list=TpaperService.selectTpaper();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }


    @RequestMapping("/selectTpaperByTid")
    public void selectTpaperByTid(HttpServletResponse response, Integer tid) throws IOException {
        List<Tpaper> list=TpaperService.selectTpaperByTid(tid);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/addTpaper")
    private void addTpaper(HttpServletResponse response, Tpaper Tpaper, String name, String tag, String brief, String img, Integer tid) throws IOException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(new Date()));
        Tpaper.setImg("http://localhost:8080/images/" + img);
        Tpaper.setName(name);
        Tpaper.setTime(df.format(new Date()));
        Tpaper.setTag(tag);
        Tpaper.setBrief(brief);
        Tpaper.setTid(tid);
        TpaperService.insertSelective(Tpaper);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/updateTpaper")
    private void updateTpaper(HttpServletResponse response, Tpaper Tpaper, String name, String tag, String brief, Integer id, String img) throws IOException {
        Tpaper.setId(id);
        Tpaper.setName(name);
        Tpaper.setTag(tag);
        Tpaper.setBrief(brief);
        Tpaper.setImg(img);
        TpaperService.updateByPrimaryKeySelective(Tpaper);
        Gson gson = new Gson();
        String json = gson.toJson("");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }


    @RequestMapping("/deleteTpaper")
    public void deleteTpaper(HttpServletResponse response, Integer id) throws IOException {
        TpaperService.deleteByPrimaryKey(id);
        Gson gson = new Gson();
        String json = gson.toJson(TpaperService.selectByPrimaryKey(id));
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTpaperByName")
    public void selectTpaperByName(HttpServletResponse response, String name) throws IOException {
        List<Tpaper> list=TpaperService.selectTpaperByName(name);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectTpaperById")
    public void selectTpaperById(HttpServletResponse response, Integer id) throws IOException {
        Gson gson = new Gson();
        String json = gson.toJson(TpaperService.selectByPrimaryKey(id));
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }
}
